

Extern "Windows-MS"
	Declare Function Folder_Copy(sPathSrc As ZString Ptr, sPathDst As ZString Ptr, sFilt As ZString Ptr, iRecu As Integer) As Integer
End Extern

Type CopyPathInfo
	DstPath As ZString Ptr
	Filter As ZString Ptr
	FileCount As UInteger
	IsRecu As Integer
End Type



Function ScanFile_Proc(Path As ZString Ptr, FindData As WIN32_FIND_DATA Ptr, param As Integer = 0) As Integer
	If param And 4 Then
		If FindData->dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
			Return 0
		EndIf
	EndIf
	AutoFreeBuffer.PutData(*Path & FindData->cFileName & !"\r\n")
End Function

Function ScanName_Proc(Path As ZString Ptr, FindData As WIN32_FIND_DATA Ptr, param As Integer = 0) As Integer
	If param And 4 Then
		If FindData->dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
			Return 0
		EndIf
	EndIf
	AutoFreeBuffer.PutData(FindData->cFileName & !"\r\n")
End Function

Function CopyPath_Proc(Path As ZString Ptr, FindData As WIN32_FIND_DATA Ptr, param As Integer = 0) As Integer
	Dim pInfo As CopyPathInfo Ptr = Cast(Any Ptr, param)
	If FindData->dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
		pInfo->FileCount += Folder_Copy(*Path & FindData->cFileName & "\", *pInfo->DstPath & FindData->cFileName & "\", pInfo->Filter, pInfo->IsRecu)
	Else
		CopyFile(*Path & FindData->cFileName, *pInfo->DstPath & FindData->cFileName, FALSE)
		pInfo->FileCount += 1
	EndIf
	Return 0
End Function

Extern "Windows-MS"
	
	Function File_List(sPath As ZString Ptr, sFilt As ZString Ptr, iAttr As Integer, iAtex As Integer, iRecu As Integer) As ZString Ptr Export
		AutoFreeBuffer.FreeMemory()
		xRtl.File.Scan(sPath, sFilt, iAttr, iAtex, iRecu, @ScanFile_Proc, iAtex)
		If AutoFreeBuffer.BufferMemory Then
			AutoFreeBuffer.BufferMemory[AutoFreeBuffer.BufferUseLen - 2] = 0
			Return AutoFreeBuffer.BufferMemory
		Else
			Return @xRtl.xywh_library_xrtl_memory_snull
		EndIf
	End Function
	
	Function File_ListName(sPath As ZString Ptr, sFilt As ZString Ptr, iAttr As Integer, iAtex As Integer) As ZString Ptr Export
		AutoFreeBuffer.FreeMemory()
		xRtl.File.Scan(sPath, sFilt, iAttr, iAtex, 0, @ScanName_Proc, iAtex)
		If AutoFreeBuffer.BufferMemory Then
			AutoFreeBuffer.BufferMemory[AutoFreeBuffer.BufferUseLen - 2] = 0
			Return AutoFreeBuffer.BufferMemory
		Else
			Return @xRtl.xywh_library_xrtl_memory_snull
		EndIf
	End Function
	
	Function File_Read(sPath As ZString Ptr) As ZString Ptr Export
		Dim TempPtr As ZString Ptr
		If xRtl.File.eRead(sPath, @TempPtr) Then
			Return TempPtr
		Else
			Return @xRtl.xywh_library_xrtl_memory_snull
		EndIf
	End Function
	
	Function File_Write(sPath As ZString Ptr, sText As ZString Ptr, iSize As UInteger) As UInteger Export
		If iSize = 0 Then
			iSize = strlen(sText)
		EndIf
		Function = xRtl.File.Write(sPath, sText, 0, iSize)
		xRtl.File.Cut(sPath, iSize)
	End Function
	
	Function File_Append(sPath As ZString Ptr, sText As ZString Ptr, iSize As UInteger) As UInteger Export
		If iSize = 0 Then
			iSize = strlen(sText)
		EndIf
		Dim iLen As UInteger = xRtl.File.Size(sPath)
		Return xRtl.File.Write(sPath, sText, iLen, iSize)
	End Function
	
	Function xFile_Create Alias "File_Create" (sPath As ZString Ptr) As Integer Export
		Return xRtl.File.Create(sPath)
	End Function
	
	Function File_Copy(sPathSrc As ZString Ptr, sPathDst As ZString Ptr) As Integer Export
		Return CopyFile(sPathSrc, sPathDst, FALSE)
	End Function
	
	Function File_Move(sPathSrc As ZString Ptr, sPathDst As ZString Ptr) As Integer Export
		Return MoveFileEx(sPathSrc, sPathDst, MOVEFILE_REPLACE_EXISTING Or MOVEFILE_COPY_ALLOWED Or MOVEFILE_WRITE_THROUGH)
	End Function
	
	Function File_Delete(sPath As ZString Ptr) As Integer Export
		Return DeleteFile(sPath)
	End Function
	
	Function File_Exists(sPath As ZString Ptr) As Integer Export
		Return xRtl.File.Exists(sPath)
	End Function
	
	Function File_GetAttr(sPath As ZString Ptr) As UInteger Export
		Return GetFileAttributes(sPath)
	End Function
	
	Function File_SetAttr(sPath As ZString Ptr, uVal As UInteger) As Integer Export
		Return SetFileAttributes(sPath, uVal)
	End Function
	
	Function Folder_Create(sPath As ZString Ptr) As Integer Export
		Dim iLen As UInteger = strlen(sPath)
		For i As Integer = 0 To iLen - 1
			Select Case Cast(UByte Ptr, sPath)[i]
				Case 58
					i += 1
				Case 92, 47
					Dim bTmp As UByte = Cast(UByte Ptr, sPath)[i]
					Cast(UByte Ptr, sPath)[i] = 0
					CreateDirectory(sPath, NULL)
					Cast(UByte Ptr, sPath)[i] = bTmp
			End Select
		Next
		Return CreateDirectory(sPath, NULL)
	End Function
	
	Function Folder_Copy(sPathSrc As ZString Ptr, sPathDst As ZString Ptr, sFilt As ZString Ptr, iRecu As Integer) As Integer Export
		Dim Info As CopyPathInfo
		Info.DstPath = sPathDst
		Info.Filter = sFilt
		Info.FileCount = 0
		Info.IsRecu = iRecu
		Folder_Create(sPathDst)
		xRtl.File.Scan(sPathSrc, sFilt, 0, 0, 0, @CopyPath_Proc, Cast(Integer, @Info))
		Return Info.FileCount
	End Function
	
End Extern
